<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>lreplace manual page - Tcl Built-In Commands</TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">Tcl Commands</a> <small>&gt;</small> lreplace</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<H3><A NAME="M2">NAME</A></H3>
lreplace &mdash; Replace elements in a list with new elements
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>lreplace </B><I>list first last </I>?<I>element element ...</I>?<BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
<B>lreplace</B> returns a new list formed by replacing zero or more elements of
<I>list</I> with the <I>element</I> arguments.
<I>first</I> and <I>last</I> are index values specifying the first and
last elements of the range to replace.
The index values <I>first</I> and <I>last</I> are interpreted
the same as index values for the command <B><A HREF="../TclCmd/string.htm">string index</A></B>,
supporting simple index arithmetic and indices relative to the
end of the list.
0 refers to the first element of the
list, and <B>end</B> refers to the last element of the list.
<P>
If either <I>first</I> or <I>last</I> is less than zero, it is considered
to refer to before the first element of the list. This allows <B>lreplace</B>
to prepend elements to <I>list</I>.
If either <I>first</I> or <I>last</I> indicates a position greater than the
index of the last element of the list, it is treated as if it is an
index one greater than the last element. This allows <B>lreplace</B> to
append elements to <I>list</I>.
<P>
If <I>last</I> is less than <I>first</I>, then any specified elements
will be inserted into the list before the element specified by <I>first</I>
with no elements being deleted.
<P>
The <I>element</I> arguments specify zero or more new elements to
be added to the list in place of those that were deleted.
Each <I>element</I> argument will become a separate element of
the list.  If no <I>element</I> arguments are specified, then the elements
between <I>first</I> and <I>last</I> are simply deleted.
<H3><A NAME="M5">EXAMPLES</A></H3>
Replacing an element of a list with another:
<P>
<PRE>% <B>lreplace</B> {a b c d e} 1 1 foo
a foo c d e</PRE>
<P>
Replacing two elements of a list with three:
<P>
<PRE>% <B>lreplace</B> {a b c d e} 1 2 three more elements
a three more elements d e</PRE>
<P>
Deleting the last element from a list in a variable:
<P>
<PRE>% set var {a b c d e}
a b c d e
% set var [<B>lreplace</B> $var end end]
a b c d</PRE>
<P>
A procedure to delete a given element from a list:
<P>
<PRE>proc lremove {listVariable value} {
    upvar 1 $listVariable var
    set idx [lsearch -exact $var $value]
    set var [<B>lreplace</B> $var $idx $idx]
}</PRE>
<P>
Appending elements to the list; note that <B>end+2</B> will initially
be treated as if it is <B>6</B> here, but both that and <B>12345</B> are greater
than the index of the final item so they behave identically:
<P>
<PRE>% set var {a b c d e}
a b c d e
% set var [<B>lreplace</B> $var 12345 end+2 f g h i]
a b c d e f g h i</PRE>
<H3><A NAME="M6">SEE ALSO</A></H3>
<B><A HREF="../TclCmd/list.htm">list</A></B>, <B><A HREF="../TclCmd/lappend.htm">lappend</A></B>, <B><A HREF="../TclCmd/lindex.htm">lindex</A></B>, <B><A HREF="../TclCmd/linsert.htm">linsert</A></B>, <B><A HREF="../TclCmd/llength.htm">llength</A></B>, <B><A HREF="../TclCmd/lsearch.htm">lsearch</A></B>, <B><A HREF="../TclCmd/lset.htm">lset</A></B>, <B><A HREF="../TclCmd/lrange.htm">lrange</A></B>, <B><A HREF="../TclCmd/lsort.htm">lsort</A></B>, <B><A HREF="../TclCmd/string.htm">string</A></B>
<H3><A NAME="M7">KEYWORDS</A></H3>
<A href="../Keywords/E.htm#element">element</A>, <A href="../Keywords/L.htm#list">list</A>, <A href="../Keywords/R.htm#replace">replace</A>
<div class="copy">Copyright &copy; 1993 The Regents of the University of California.
<BR>Copyright &copy; 1994-1996 Sun Microsystems, Inc.
<BR>Copyright &copy; 2001 Kevin B. Kenny &lt;kennykb(at)acm.org&gt;. All rights reserved.
</div>
</BODY></HTML>
